package com.jmodata.zxu.controller.manager;

import com.jmodata.zxu.model.Tree;
import com.jmodata.zxu.pojo.manager.Manager;
import com.jmodata.zxu.pojo.manager.Menu;
import com.jmodata.zxu.service.manager.MenuService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("menu")
public class MenuController {

	@Autowired(required = false)
	private MenuService menuService;
	
	/**
	 * 跳转到菜单首�?
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("")
	public String moduleList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "menu/menu";
	}
	
	/**
	 * 跳转到添加页�?
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/toaddmenu")
	public String toAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "menu/menuForm";
	}
	
	/**
	 * 通过菜单编号查询对象
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/getmenu")
	public void toEdit(HttpServletRequest request, HttpServletResponse response, String menuid) throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		Menu menu = null;
		if(null != menuid && !"".equals(menuid)){
			menu = menuService.getMenuById(menuid);
			if(null != menu){
				out.print(json.fromObject(menu));
			}
		}
		
	}
	
	/**
	 * 添加菜单
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addmenu")
	public void addMenu(HttpServletRequest request, HttpServletResponse response, Menu menu) throws Exception{
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		menu.setCreate_time(new Date());
		int flag =menuService.insertMenu(menu);
		if(flag>0){
			json.put("success", true);
			json.put("msg", "添加菜单成功!");
		} else {
			json.put("success", "false");
			json.put("msg", "添加菜单失败!");
		}
		out.print(json);
	}
	
	/**
	 * 查询左边树菜�?
	 * @param request
	 * @param response
	 * @param id 子节点的父节点ID
	 * @throws Exception
	 */
	@RequestMapping("/menutree")
	public void queryMenuTree(HttpServletRequest request, HttpServletResponse response, String id) throws Exception{
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		JSONArray json = new JSONArray();
		List<Tree> treeList = new ArrayList<Tree>();
		Manager manager = (Manager)request.getSession().getAttribute("mgr");
		treeList = menuService.queryMenuTreeByManagerId(manager,id);
		if(null != treeList && treeList.size()>0){
			out.print(json.fromObject(treeList));
		}
	}
	
	/**
	 * 查询菜单列表
	 * @param request
	 * @param response
	 * @param menu
	 * @throws Exception
	 */
	@RequestMapping("/menuList")
	public void queryMenuList(HttpServletRequest request, HttpServletResponse response, Menu menu, String id) throws Exception{
		response.setCharacterEncoding("UTF-8");
		JSONArray json = new JSONArray();
		List<Tree> treeList = new ArrayList<Tree>();
		treeList = menuService.queryMenuList(menu,id);
		if(null != treeList && treeList.size()>0){
			response.getWriter().print(json.fromObject(treeList));
		}else{
			response.getWriter().print("");
		}
	}
	
	/**
	 * 修改菜单
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/editmenu")
	public void editMenu(HttpServletRequest request, HttpServletResponse response, Menu menu) throws Exception{
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		int flag = menuService.updateMenu(menu);
		if(flag>0){
			json.put("success", true);
			json.put("msg", "修改菜单成功!");
		} else {
			json.put("success", "false");
			json.put("msg", "修改菜单失败!");
		}
		out.print(json);
	}
	/**
	 * 删除菜单
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delmenu")
	public void deleteMenu(HttpServletRequest request, HttpServletResponse response, String menuids) throws Exception{
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		boolean flag = false;
		try {
			if(menuids.indexOf(",") != -1){
				String[] menu_ids = menuids.split(",");
				for (int i = 0; i < menu_ids.length; i++) {
					menuService.deleteMenu(new BigDecimal(menu_ids[i]));
				}
			}else{
				menuService.deleteMenu(new BigDecimal(menuids.toString()));
			}
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(flag){
			json.put("success", true);
			json.put("msg","删除菜单成功!");
		} else {
			json.put("success", "false");
			json.put("msg", "删除菜单失败!");
		}
		out.print(json);
	}
}
